from typing import Dict, Any

from fastapi import APIRouter, Request, Depends, HTTPException
from sqlalchemy.ext.asyncio import AsyncSession

from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_kylin.entity.vo.future_trade_cal_vo import (
    FutureTradeCalPageQueryModel,
    DeleteFutureTradeCalModel,
    UpdateFutureTradeCalModel
)
from module_kylin.service.future_trade_cal_service import FutureTradeCalService
from utils.response_util import ResponseUtil

# 创建路由器
futureTradeCalController = APIRouter(
    prefix="/kylin/tradeCal",
    tags=["期货交易日历管理"]
)


@futureTradeCalController.get('/list', dependencies=[Depends(CheckUserInterfaceAuth('kylin:tradeCal:list'))])
async def get_future_trade_cal_list(
        request: Request,
        page_query_model: FutureTradeCalPageQueryModel = Depends(FutureTradeCalPageQueryModel.as_query),
        query_db: AsyncSession = Depends(get_db),
):
    """
    获取期货交易日历列表
    """
    try:
        # 获取分页数据
        trade_cal_page_query_result = await FutureTradeCalService.get_trade_cal_list_services(
            query_db, page_query_model, is_page=True
        )
        return ResponseUtil.success(model_content=trade_cal_page_query_result)

    except Exception as e:
        raise HTTPException(status_code=500, detail=f'获取失败: {str(e)}')


@futureTradeCalController.post('', dependencies=[Depends(CheckUserInterfaceAuth('kylin:tradeCal:add'))])
@Log(title='期货交易日历管理', business_type=BusinessType.INSERT)
async def add_future_trade_cal(
        request: Request,
        trade_cal_data: Dict[str, Any],
        query_db: AsyncSession = Depends(get_db),
):
    """
    添加期货交易日历
    """
    try:
        # 添加交易日历
        result = await FutureTradeCalService.add_trade_cal_services(query_db, trade_cal_data)
        # 检查是否成功
        if result.get('success'):
            return ResponseUtil.success(msg=result.get('message', '添加成功'))
        else:
            return ResponseUtil.failure(msg=result.get('msg', '添加失败'))

    except Exception as e:
        raise HTTPException(status_code=500, detail=f'添加失败: {str(e)}')


@futureTradeCalController.put('', dependencies=[Depends(CheckUserInterfaceAuth('kylin:tradeCal:edit'))])
@Log(title='期货交易日历管理', business_type=BusinessType.UPDATE)
async def update_future_trade_cal(
        request: Request,
        update_data: UpdateFutureTradeCalModel,
        query_db: AsyncSession = Depends(get_db),
):
    """
    更新期货交易日历
    """
    try:
        # 更新交易日历
        result = await FutureTradeCalService.update_trade_cal_services(query_db, update_data)
        return ResponseUtil.success(msg=result.get('message', '更新成功'))

    except Exception as e:
        raise HTTPException(status_code=500, detail=f'更新失败: {str(e)}')


@futureTradeCalController.delete('', dependencies=[Depends(CheckUserInterfaceAuth('kylin:tradeCal:remove'))])
@Log(title='期货交易日历管理', business_type=BusinessType.DELETE)
async def delete_future_trade_cal(
        request: Request,
        delete_data: DeleteFutureTradeCalModel,
        query_db: AsyncSession = Depends(get_db),
):
    """
    删除期货交易日历
    """
    try:
        # 删除交易日历
        result = await FutureTradeCalService.delete_trade_cal_services(query_db, delete_data)
        return ResponseUtil.success(msg=result.get('message', '删除成功'))

    except Exception as e:
        raise HTTPException(status_code=500, detail=f'删除失败: {str(e)}')


@futureTradeCalController.post('/sync', dependencies=[Depends(CheckUserInterfaceAuth('kylin:tradeCal:sync'))])
@Log(title='期货交易日历管理', business_type=BusinessType.OTHER)
async def sync_future_trade_cal(
        request: Request,
        query_db: AsyncSession = Depends(get_db),
):
    """
    同步期货交易日历
    """
    try:
        # 同步交易日历
        result = await FutureTradeCalService.sync_tushare_data_services(query_db)
        return ResponseUtil.success(msg=result.get('message', '同步成功'))

    except Exception as e:
        raise HTTPException(status_code=500, detail=f'同步失败: {str(e)}')
